// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

/*
 *重置密码界面
*/
cc.Class({
    extends: cc.Component,

    properties: {
        labTopTitle: cc.Label,  //标题
        labPrompt: cc.Label,  //tip
        edCode: cc.EditBox,  //新的密码
        _verifycode:"",
        _type:0,//修改的类型   
        pwd:"", 
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },

    init: function(data){
        this._type = data.type;
        if(data.type == 1)
        {
            this._verifycode = data.Code;
            this.labPrompt.string = "请为您的账号设置新的登录密码";
            this.labTopTitle.string = "修改登录密码";
            this.edCode.maxLength = 18;
            //this.edCode.inputMode = EditBox.InputMode.SINGLE_LINE;
        }
        else if(data.type == 2)
        {
            this._verifycode = data.Code;
            this.labPrompt.string = "请为您的账号设置新的提现密码";
            this.labTopTitle.string = "修改提现密码";
            this.edCode.maxLength = 6;
            //this.edCode.inputMode = EditBox.InputMode.NUMERIC;
        }        
    },

    //新密码
    onTextChangedPwd:function(text, editbox, customEventData){
        var txt = text;
        if(this._type == 1)//登录密码
        {
            if(txt == "")
            {
                this.pwd = txt;
            }
            else
            {
                var re = txt.replace(/\s+/g,"");
                editbox.string = re;
                txt = re;
                this.pwd = txt;
            }
        }
        else //提现密码
        {
            if(Utils.isInt(txt)||txt == "")
                this.pwd = txt;
            else
            {
                txt = this.pwd;
                editbox.string = txt;
                return;
            }
        }
    },

    checkInput:function(){
        if(this.edCode.string != "")
        {
            if(!Utils.isPassWord(this.edCode.string))
            {
                ComponentsUtils.showTips("密码格式错误！");
                return false;
            }
        }
        else
        {
            ComponentsUtils.showTips("密码不能为空！");
            return false;
        }
        return true;
    },

    //保存
    onTrue: function(){
        if(this.checkInput())
        {
            var recv = function(ret){
                ComponentsUtils.unblock();
                if (ret.Code !== 0) {
                    cc.error(ret.Msg);
                    ComponentsUtils.showTips(ret.Msg);
                }else{
                    if(this._type == 1)
                    {
                        User.setPwd(this.edCode.string);
                        //this.onOut();
                        this.onClose();
                    }  
                    else if(this._type == 2)
                        User.setIsWithdrawPwd(true);    
                        this.onClose();
                }
            }.bind(this);

            var data = {
                Token: User.getLoginToken(),
                UserCode: User.getUserCode(),
                PwdType: this._type,//1登录密码 2支付密码
                Pwd: this.edCode.string,
                VerifyCode: this._verifycode,
            };
            CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.RESETPWDLOGIN, data, recv.bind(this),"POST");  
            ComponentsUtils.block();
        }
    },

    onOut: function(){
        User.setIsvermify(false);
        var recv = function recv(ret) { 
            ComponentsUtils.unblock();
            if(ret.Code == 0 )
            {
                User.setLoginToken(ret.Token);
                this.onClose();
            }
            else
            {
                ComponentsUtils.showTips(ret.Msg);
            }
        }.bind(this);

        var data = {
            Token:User.getLoginToken(),
            UserCode:User.getUserCode(),
            Equipment:Utils.getEquipment(),
        };
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.OUTACCOUNT, data, recv.bind(this),"POST");    
        ComponentsUtils.block();
    },

    onClose: function(){
        this.node.getComponent("Page").backAndRemove();
    },

    // update (dt) {},
});
